/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import entities.Camera;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author riccardo
 */
@Stateless
public class CameraFacade extends AbstractFacade<Camera> {

    @PersistenceContext(unitName = "WebApplication2PU")
    private EntityManager em;
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public CameraFacade() {
        super(Camera.class);
    }
    
    public List<Camera> findById(int id) {
        Query q = getEntityManager().createNamedQuery("Camera.findByIdCAMERA");
        q.setParameter("idCAMERA", id);
        return q.getResultList();
    }
    
    @Override
    public void edit(Camera entity) {
        entity.setIdCAMERA(entity.getElemento().getIdELEMENTO());
        getEntityManager().merge(entity);
    }

    public List<Camera> findByIdViaggio(int parseInt) {
        
//        Select Camera.*
//        From Composizione join Elemento on idElemento= idElemento
//                join Camera on idElemento=idCamera
//        where composizione.idViaggio = ?
        System.out.println(parseInt);
        String jql = "SELECT v FROM Composizione c INNER JOIN Fetch c.idElemento e "
                        + "INNER JOIN fetch e.camera v where (c.idViaggio.idVIAGGIO=:idViaggio)";
        System.out.println(jql);
        Query q = getEntityManager().createQuery(jql);
        q.setParameter("idViaggio", parseInt);
        return q.getResultList();
    }

    public List<Camera> findByIdPackage(int parseInt) {
        //        Select Camera.*
//        From ComposizionePacchetto join Elemento on idElemento= idElemento
//                join Camera on idElemento=idCamera
//        where composizione.idViaggio = ?
        System.out.println(parseInt);
        String jql = "SELECT v FROM Composizionepacchetto c INNER JOIN Fetch c.idElemento e "
                        + "INNER JOIN fetch e.camera v where (c.idPacchetto.idPACCHETTO=:idViaggio)";
        System.out.println(jql);
        Query q = getEntityManager().createQuery(jql);
        q.setParameter("idViaggio", parseInt);
        return q.getResultList();
    }
    
}
